package com.itheima.d2_xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileReader;
import java.util.List;

/*
     目标：掌握使用Dom4j框架解析XML文件。
 */
public class Dom4JTest1 {
    public static void main(String[] args) throws Exception {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new FileReader("user.xml"));
        Element root = document.getRootElement();
        List<Element> elements = root.elements();
        elements.forEach(element -> {
            Attribute idAttr = element.attribute("id");
            String id = idAttr.getValue();

            Element nameElement = element.element("name");
            String name = nameElement.getText();

            Element ageElement = element.element("age");
            String age = ageElement.getText();

            Element sexElement = element.element("sex");
//            String sex = sexElement.getText();
            String sex = (String) sexElement.getData();
            System.out.println(
                    "id: " + id + " name: " + name + " age: " + age + " sex: " + sex
            );
        });
    }
}
